package com.wman;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
public class GirlController {

    @Autowired
    private GirlRepository mGrilRepository;

    @Autowired
    GirlService mGirlService;

    /**
     * 查询所有女生列表
     * http://localhost:8080/wman/girls
     *
     * @return
     */
    @GetMapping(value = "/girls")
    public List<Girl> girlList() {
        return mGrilRepository.findAll();
    }

    /**
     * 创建一个女生
     * http://localhost:8080/wman/girls?age=49&cupSize=A   POST
     *
     * @param cupSize
     * @param age
     * @return
     */
    @PostMapping(value = "/girls")
    public Girl grilAdd(@RequestParam("cupSize") String cupSize,
                        @RequestParam("age") Integer age) {
        Girl gril = new Girl();
        gril.setAge(age);
        gril.setCupSize(cupSize);

        return mGrilRepository.save(gril);
    }

    /**
     * 查询一个女生
     *
     * @param id
     * @return
     */
    @GetMapping(value = "/girls/{id}")
    public Girl girlFindOne(@PathVariable(value = "id") Integer id) {
        return mGrilRepository.findOne(id);
    }

    /**
     * 查询一个女生
     *
     * @param age
     * @return
     */
    @GetMapping(value = "/girls/age/{age}")
    public List<Girl> girlFindByAge(@PathVariable(value = "age") Integer age) {
        return mGrilRepository.findByAge(age);
    }

    /**
     * 更新一个女生
     *
     * @param id
     * @return
     */
    @PutMapping(value = "/girls/{id}")
    public Girl girlUpdateOne(@PathVariable(value = "id") Integer id,
                              @RequestParam("cupSize") String cupSize,
                              @RequestParam("age") Integer age) {
        Girl girl = new Girl();
        girl.setId(id);
        girl.setCupSize(cupSize);
        girl.setAge(age);

        return mGrilRepository.save(girl);
    }

    /**
     * 删除一个女生
     *
     * @param id
     * @return
     */
    @DeleteMapping(value = "/girls/{id}")
    public String girlDeleteOne(@PathVariable(value = "id") Integer id) {
        mGrilRepository.delete(id);
        return "{\"删除成功\"}";
    }

    @PostMapping(value = "/girls/two")
    public void girlTwo() {
        mGirlService.insertTwo();
    }
}
